const PACKAGE_INFO_KEY = '__package_info__'

function handleUpdate(option) {
	uni.setStorageSync(PACKAGE_INFO_KEY, option)
	uni.navigateTo({
		url: `/pages/upgrade-popup/index?local_storage_key=${PACKAGE_INFO_KEY}`,
		fail: (err) => {
			console.error('更新弹框跳转失败', err)
			uni.removeStorageSync(PACKAGE_INFO_KEY)
		}
	})
}

/**
 * 对比版本号，如需要，请自行修改判断规则
 * 支持比对	("3.0.0.0.0.1.0.1", "3.0.0.0.0.1")	("3.0.0.1", "3.0")	("3.1.1", "3.1.1.1") 之类的
 * @param {Object} v1
 * @param {Object} v2
 * v1 > v2 return 1  返回false
 * v1 < v2 return -1  返回true
 * v1 == v2 return 0  返回true
 */
function compare(v1 = '0', v2 = '0') {
	v1 = String(v1).split('.')
	v2 = String(v2).split('.')
	const minVersionLens = Math.min(v1.length, v2.length);

	let result = 0;
	for (let i = 0; i < minVersionLens; i++) {
		const curV1 = Number(v1[i])
		const curV2 = Number(v2[i])

		if (curV1 > curV2) {
			result = 1
			break;
		} else if (curV1 < curV2) {
			result = -1
			break;
		}
	}

	if (result === 0 && (v1.length !== v2.length)) {
		const v1BiggerThenv2 = v1.length > v2.length;
		const maxLensVersion = v1BiggerThenv2 ? v1 : v2;
		for (let i = minVersionLens; i < maxLensVersion.length; i++) {
			const curVersion = Number(maxLensVersion[i])
			if (curVersion > 0) {
				v1BiggerThenv2 ? result = 1 : result = -1
				break;
			}
		}
	}
	if (result < 1) {
		return true
	} else {
		return false
	}
}
export default function autoUpdate(vm) {
	let platform = uni.getSystemInfoSync().platform
	plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
		vm.$u.api.whole.getEditionInfo(platform).then(({
			data
		}) => {
			let versionInfo = data
			if (!versionInfo) {
				// 没获取到版本信息,不需要做处理
				return
			}
			// let curVersion = '3.0.0';
			let curVersion= widgetInfo.version;
			console.log("versionInfo:", versionInfo)
			let inVersion = compare(versionInfo.version, curVersion);
			let inApktolerant = compare(versionInfo.tolerantApkVersion, curVersion);
			let inHottolerant=true;
			 if(versionInfo.tolerantHotUpgradeVersion){
				inHottolerant = compare(versionInfo.tolerantHotUpgradeVersion, curVersion);
			 }
		 
			console.log("inVersion:", inVersion)
			console.log("inApktolerant:", inApktolerant)
			console.log("inHottolerant:", inHottolerant)
			if (inVersion) {
				// 服务器版本小于等于当前版本,不需要做处理
				return
			}
			
			let updateOption = {}
			if (inApktolerant) {
				// 服 apk 容忍版本＜=本version
				if (versionInfo.upgradeType == 1) {
					if (inHottolerant) {
						// 热更新容忍版本＜=本 version  提示更新内容，选择性更新，且弹窗只出现一次
						updateOption = {
							url: versionInfo.hotUpgradeUrl,
							type: 'wgt',
							is_mandatory: false,
						}
					} else {
						// 服 热更新容忍版本>本 version  提示更新内容，强制更新
						updateOption = {
							url: versionInfo.hotUpgradeUrl,
							type: 'wgt',
							is_mandatory: true,
						}
					}
				} else if (versionInfo.upgradeType == 2) {
					// 全量软更新:提示更新内容，选择性更新，弹窗每次都出现
					updateOption = {
						url: versionInfo.apkDownloadUrl,
						type: 'all',
						is_mandatory: false,
					}
				}
			} else {
				updateOption = {
					url: versionInfo.apkDownloadUrl,
					type: 'all',
					is_mandatory: true,
				}
			}
			
			updateOption = Object.assign({}, updateOption, {
				platform: platform,
				contents: versionInfo.upgradeDescription,
				version: versionInfo.version,
			})
			console.log(updateOption)
			handleUpdate(updateOption)
		})
	})
}
